package com.yunkeji.system.event.impl;

import cn.hutool.core.bean.BeanUtil;
import com.yunkeji.common.core.domain.event.PayLogEvent;
import com.yunkeji.dao.domain.AppUser;
import com.yunkeji.dao.domain.AppUserExt;
import com.yunkeji.dao.domain.PayLog;
import com.yunkeji.dao.mapper.AppUserExtMapper;
import com.yunkeji.dao.mapper.AppUserMapper;
import com.yunkeji.dao.mapper.PayLogMapper;
import com.yunkeji.dao.mapper.sys.SysLogininforMapper;
import com.yunkeji.system.event.service.LogEventService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * 事件实现
 *
 * @author 老张
 */
@RequiredArgsConstructor
@Slf4j
@Service
public class LogEventServiceImpl implements LogEventService {
    private final SysLogininforMapper baseMapper;
    private final AppUserMapper appUserMapper;
    private final AppUserExtMapper appUserExtMapper;
    private final PayLogMapper payLogMapper;

    /**
     * 记录支付信息
     *
     * @author 老张
     * @param payLogEvent 支付事件
     */
    @Async
    @EventListener
    @Override
    public void payLogEvent(PayLogEvent payLogEvent) {
        log.info("支付日志事件->{}", payLogEvent.toString());
        Long userId = payLogEvent.getUserId();
        AppUser appUser = appUserMapper.selectById(userId);
        AppUserExt appUserExt = appUserExtMapper.selectById(userId);
        PayLog payLog = BeanUtil.toBean(payLogEvent, PayLog.class);
        payLog.setPlatformId(appUser.getPlatformId())
                .setPhone(appUser.getPhone())
                .setNickName(appUserExt.getNickName())
                .setHeadUrl(appUserExt.getHeadUrl());

        payLogMapper.insert(payLog);
    }


}
